import 'package:flutter/material.dart';
import '../../domain/models/device_model.dart';

/// 设备状态管理提供者
class DeviceProvider extends ChangeNotifier {
  List<DeviceModel> _devices = [];
  List<RoomModel> _rooms = [];
  String _selectedRoom = '全部';
  bool _isEditMode = false;

  List<DeviceModel> get devices => _devices;
  List<RoomModel> get rooms => _rooms;
  String get selectedRoom => _selectedRoom;
  bool get isEditMode => _isEditMode;

  /// 初始化设备和房间数据
  void initData() {
    // 初始化房间数据
    _rooms = [
      RoomModel(id: '1', name: '全部', icon: Icons.home),
      RoomModel(id: '2', name: '客厅', icon: Icons.weekend),
      RoomModel(id: '3', name: '厨房', icon: Icons.kitchen),
      RoomModel(id: '4', name: '卧室', icon: Icons.bed),
      RoomModel(id: '5', name: '其他', icon: Icons.more_horiz),
    ];

    // 初始化设备数据
    _devices = [
      DeviceModel(
        id: '1',
        name: '客厅 空调',
        roomName: '客厅',
        type: DeviceType.airConditioner,
        imageUrl: 'assets/images/living_room.jpg',
        isOn: true,
        properties: {
          'mode': '制热+地暖',
          'temperature': 26,
          'fanSpeed': '超低风',
        },
      ),
      DeviceModel(
        id: '2',
        name: '厨房 空调',
        roomName: '厨房',
        type: DeviceType.airConditioner,
        imageUrl: 'assets/images/kitchen.jpg',
        isOn: false,
        properties: {
          'mode': '送风',
          'temperature': 26,
          'fanSpeed': '中风',
        },
      ),
      DeviceModel(
        id: '3',
        name: '客厅 空气盒子',
        roomName: '客厅',
        type: DeviceType.airPurifier,
        imageUrl: 'assets/images/living_room.jpg',
        isOn: true,
        properties: {
          'temperature': 26,
          'humidity': '35%',
          'pm25': '120μg/m3',
        },
      ),
      DeviceModel(
        id: '4',
        name: '卧室 空调',
        roomName: '卧室',
        type: DeviceType.airConditioner,
        imageUrl: 'assets/images/bedroom.jpg',
        isOn: false,
        properties: {
          'mode': '送风',
          'temperature': 26,
          'fanSpeed': '中风',
        },
      ),
    ];

    notifyListeners();
  }

  /// 切换设备开关状态
  void toggleDeviceStatus(String deviceId) {
    final index = _devices.indexWhere((device) => device.id == deviceId);
    if (index != -1) {
      _devices[index] = _devices[index].copyWith(isOn: !_devices[index].isOn);
      notifyListeners();
    }
  }

  /// 选择房间
  void selectRoom(String roomName) {
    _selectedRoom = roomName;
    notifyListeners();
  }

  /// 切换编辑模式
  void toggleEditMode() {
    _isEditMode = !_isEditMode;
    notifyListeners();
  }

  /// 重新排序设备
  void reorderDevices(int oldIndex, int newIndex) {
    if (oldIndex < newIndex) {
      newIndex -= 1;
    }
    final DeviceModel item = _devices.removeAt(oldIndex);
    _devices.insert(newIndex, item);
    notifyListeners();
  }

  /// 获取指定房间的设备
  List<DeviceModel> getDevicesByRoom() {
    if (_selectedRoom == '全部') {
      return _devices;
    } else {
      return _devices.where((device) => device.roomName == _selectedRoom).toList();
    }
  }
}